import traer.physics.*;

float _x,_y,_px,_py,_vx,_vy;

class Player {
  int x, y;
  int w, h;

  traer.physics.ParticleSystem physics;
  traer.physics.Particle[] particles;

  void init(int i) {
    physics = new traer.physics.ParticleSystem(5.0,0.05);
    particles = new traer.physics.Particle[1];
    particles[0] = physics.makeParticle(2,width/2,height/2,0);
    particles[0].makeFixed();
    physics.setGravity(0,0,0);
  }

  void init() {
    this.x = width/2;
    this.y = height/2;
    this.w = this.h = 4;
    println("X: "+this.x+" Y: "+this.y);
  }

  void updatePhysics() {

    PGraphicsOpenGL pgl = (PGraphicsOpenGL) g;
    GL gl = pgl.beginGL();

    _px = particles[0].position().x();
    _py = particles[0].position().y();
    physics.advanceTime(1.0);
    _x = particles[0].position().x();
    _y = particles[0].position().y();
    _vx = (_x-_px)*invWidth;
    _vy = (_y-_py)*invHeight;

    gl.glEnable(GL.GL_BLEND);
    //fadeToColor(gl,0,0,0,0.05);
    //fadeBottom(gl);
    gl.glBlendFunc(GL.GL_ONE, GL.GL_ONE);
    gl.glEnable(GL.GL_LINE_SMOOTH);
    gl.glLineWidth(1);

    checkSides();
    gl.glBegin(GL.GL_QUADS);

    addForce(_x*invWidth,_y*invHeight*1.25,_vx,_vy);
    addForce(_x*invWidth,_y*invHeight*1.25,_vx,_vy);
    addForce(_x*invWidth,_y*invHeight*1.25,_vx,_vy);
    addForce(_x*invWidth,_y*invHeight*1.25,_vx,_vy);

    drawOldSchool(gl);
    gl.glEnd();
    gl.glDisable(GL.GL_BLEND);
    pgl.endGL();


  }

  void fadeBottom(GL gl) {
    gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);
    gl.glColor4f(0,0,0,1);
    gl.glBegin(GL.GL_QUADS);
    gl.glVertex2f(0,height*.95);
    gl.glVertex2f(width, height*.95);
    gl.glVertex2f(width,height);
    gl.glVertex2f(0,height);
    gl.glEnd();
  }

  void drawOldSchool(GL gl) {
    gl.glColor3f(1,1,1);
    gl.glVertex2f(particles[0].position().x()-4,particles[0].position().y()-4);
    gl.glVertex2f(particles[0].position().x()+4,particles[0].position().y()-4);
    gl.glVertex2f(particles[0].position().x()+4,particles[0].position().y()+4);
    gl.glVertex2f(particles[0].position().x()-4,particles[0].position().y()+4);
    /*gl.glColor3f(1.0f,1.0f,1.0f);
     gl.glVertex2i(x-w,y-h);
     gl.glVertex2i(x+w,y-h);
     gl.glVertex2i(x+w,y+h);
     gl.glVertex2i(x-w,y+h);*/
  }

  void move() {
    float px, py, vx, vy;
    px = pmouseX;
    py = pmouseY;
    this.x = mouseX;
    this.y = mouseY;
    vx = (((float)x-px)*invWidth);
    vy = (((float)y-py)*invHeight);
    addForce(x*invWidth,y*invHeight*1.25,vx,vy);
  }

  void move(int x, int y) {
    float px, py, vx, vy;
    px = this.x;
    py = this.y;
    this.x = x;
    if(this.x+this.w > width) this.x = width-this.w;
    if(this.x-this.w < 0) this.x = 0+this.w;
    this.y = y;
    if(this.y+this.h > height*.8) this.y = (int)(height*.8-this.h);
    if(this.y-this.h < 0) this.y = 0+this.h;
    vx = (((float)x-px)*invWidth);
    vy = (((float)y-py)*invHeight);
    addForce(x*invWidth,y*invHeight*1.25,vx,vy);
  }

  void checkSides() {
    if(particles[0].position().x() > width) {
      particles[0].moveTo(width,particles[0].position().y(),0);
      particles[0].setVelocity(0,particles[0].velocity().y(),0);
    }
    if(particles[0].position().x() < 0 ) {
      particles[0].moveTo(0,particles[0].position().y(),0);
      particles[0].setVelocity(0,particles[0].velocity().y(),0);
    }
    if(particles[0].position().y() > height*.8) {
      particles[0].moveTo(particles[0].position().x(),height*.8,0);
      particles[0].setVelocity(particles[0].velocity().x(),0,0);
    }
    if(particles[0].position().y() < 0 ) {
      particles[0].moveTo(particles[0].position().x(),0,0);
      particles[0].setVelocity(particles[0].velocity().x(),0,0);
    }
  }

  void shoot(int i) {
    shot.spawnshot(particles[i]);
  }


}
















